package com.topvision.jack.camerasdk2.camera.view;


import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.topvision.jack.camerasdk2.camera.callback.OnCameraListener;
import com.topvision.jack.camerasdk2.camera.VideoRecorder;
import com.topvision.jack.camerasdk2.camera.commom.CameraConstants;
import com.topvision.jack.camerasdk2.camera.utils.CameraUtils;

import java.io.IOException;

/**
 * Camera预览显示
 * 创建时间:2017/12/20
 * 作者 jack
 * 描述
 */
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

    public final String TAG = CameraSurfaceView.class.getSimpleName();
    SurfaceHolder surfaceHolder;
    private Camera mCamera;
    OnCameraListener onCameraListener;
    private int mFormat;
    int degree;


    public CameraSurfaceView(Context context) {
        super(context);

    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        this.surfaceHolder = surfaceHolder;
        Log.e(TAG, "surfaceCreated");
        if (this.openCamera()) {
            Log.e(this.TAG, "preview size = " + this.mCamera.getParameters().getPreviewSize().width + "x" + this.mCamera.getParameters().getPreviewSize().height);
            if (this.onCameraListener != null) {
                try{
                    this.mCamera.startPreview();
                    Log.e(CameraConstants.TAG,"camera startPreview");
                }catch (Exception e){
                  e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
        Log.e(TAG, "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.e(TAG, "surfaceDestroyed");
        if (this.mCamera != null) {
            getHolder().removeCallback(this);
            this.mCamera.setPreviewCallbackWithBuffer(null);
            this.mCamera.stopPreview();
            this.mCamera.release();
            this.mCamera = null;

//            getHolder().removeCallback(this);
//            mCamera.setPreviewCallback(null);
//            //新增的
//            mCamera.cancelAutoFocus();
//            mCamera.stopPreview();
//            mCamera.lock();
//            mCamera.release();
//            mCamera = null;
        }
    }

    public void onPause() {
        try {
            if (mCamera != null) {
                //触发surfaceDestroyed,释放camera资源
                mCamera.setPreviewDisplay(null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onResume() {
        getHolder().addCallback(this);
    }


    private boolean openCamera() {

        try {
            if (this.mCamera != null) {
                Log.e(CameraConstants.TAG,"camera reconnect");
                this.mCamera.reconnect();
            } else if (this.onCameraListener != null) {
                this.mCamera = this.onCameraListener.setupCamera();
            }

            if (this.mCamera != null) {
                this.mCamera.setPreviewDisplay(this.getHolder());
                this.mCamera.setDisplayOrientation(degree);
                Camera.Parameters parameters = mCamera.getParameters();
                //设置对象模式
                if (CameraUtils.isSupportFocusMode(parameters, Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
                    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                }
                int bestPreviewWidth = parameters.getPreviewSize().width;
                int bestPreviewHeight = parameters.getPreviewSize().height;
                this.mFormat = parameters.getPreviewFormat();
                int lineBytes = bestPreviewWidth * ImageFormat.getBitsPerPixel(this.mFormat) / 8;
                this.mCamera.addCallbackBuffer(new byte[lineBytes * bestPreviewHeight]);
                this.mCamera.setPreviewCallbackWithBuffer(this);
                return true;
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return false;
    }

    public static long firstPreviewTime = 0;

    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        if (firstPreviewTime == 0) {
            firstPreviewTime = System.currentTimeMillis();
        }
    }

    public void setOnCameraListener(OnCameraListener onCameraListener) {
        this.onCameraListener = onCameraListener;
    }

    //设置为竖屏模式
    public void setPortraitMode() {
        this.degree = 90;
    }

    //开启录制
    public void startRecord(Context context, String outputFilePath, int resolution) {
        //准备录制视频的一些配置
        if (VideoRecorder.getInstance().prepareVideoRecorder(context, mCamera, getHolder(), outputFilePath, resolution)) {
            VideoRecorder.getInstance().startRecord(context);
        }
    }

    //关闭录制
    public void stopRecord() {
        VideoRecorder.getInstance().stopRecord(mCamera);
    }

    public void pauseRecord(String outputPath) {
        VideoRecorder.getInstance().pauseRecord(mCamera, outputPath);
    }


    public void resumeRecord() {
        VideoRecorder.getInstance().resumeRecord(mCamera);
    }


}
